package cn.com.se.多线程.lock;

import java.util.concurrent.atomic.AtomicReference;

/**
 * 简单自旋锁实现
 */
public class SpinLock {


    private AtomicReference<Thread> sign =new AtomicReference<>();

    public void lock(){
        Thread current = Thread.currentThread();
        while(!sign .compareAndSet(null, current)){
        }
    }

    public void unlock (){
        Thread current = Thread.currentThread();
        sign .compareAndSet(current, null);
    }
}
